/* This is a example of how the minimax algorimte will search 
   for the best position given that the defender (keeper) doesn't
   move. This algortime should converge on a certain location */

#include "ai.h"
#include "location.h"
#include "myObserver.h"
#include "util2.h"



using namespace std;

int main()
{
	int teller=0;
	int x,y,r;	//location own_robot after update
	x = 1700;
	y = 400;
	r = 90;	

	int x1,y1,r1;

	Image img;
	char img_attack[20];


	
	
	Ai *robot_ai = new Ai();
	Element *testN;			//eerste element aanmaken
	
	while(1)
	{
		//foto maken
		sprintf (img_attack, "attack_image%d.pgm",teller++);

		x1 = 100;
		y1 = 900;
		r1 = 0;

		testN = new Element();
		testN->enemy_robot.setTestLocation(x1, y1, r1);	//voor hun eigen goal
		testN->own_robot.setTestLocation(x, y, r);		//voor onze goal
		testN->last_move = 5;
        	testN->depth = 0; 

		robot_ai->minimax_a(testN, 0, 100);	
		//minimax starten
		//move_size bepaalt de grote van de sprong dat 1 zet verder kan doen.
	
		//Huidige en next location printen
		printf("Present location :(%d, %d, %d), cost = %f \n",testN->own_robot.getX(), testN->own_robot.getY(), testN->own_robot.getRotation(), testN->cost);
		printf("Next location    :(%d, %d, %d), cost = %f \n",testN->child->own_robot.getX(), testN->child->own_robot.getY(), testN->child->own_robot.getRotation(),testN->child->cost);
		printf("-----------------------------------------\n");
		
		img = CreateImage(1800, 1500);
		//lijnen rond het veld tekenen
		int i;
		
		for(i=0;i<20;i++)
		{
			DrawLine(img, i,0,i,1500-1);
			DrawLine(img, 1800-1-i,0,1800-i-1,1500-1);

			DrawLine(img, 0,i,1800-1,i);
			DrawLine(img, 0,1500-i-1,1800-1,1500-i-1);
		}

		for(i=0;i<50;i++)
		{
			DrawLine(img, i,750-225,i,750+225);
			DrawLine(img, 1800-1-i,750-225,1800-i-1,750+225);
		}


		
		int j;
		for(i=0;i<1800;i++)
		{
			for(j=0;j<1500;j++)
			{
				if( robot_ai->parabool( robot_ai->dist(i,j,0,750)) == 0 )
				{
					img->pixels[i][j] = 0.75;
				 }
			}
		}
		//draw robot
		for(i=testN->own_robot.getX()-75;i<testN->own_robot.getX()+75;i++)
		{
			for(j=testN->own_robot.getY()-75;j<testN->own_robot.getY()+75;j++)
			{
				img->pixels[i][j] = 0.50;
			}
		}

		for(i=testN->enemy_robot.getX()-75;i<testN->enemy_robot.getX()+75;i++)
		{
			for(j=testN->enemy_robot.getY()-75  ;j<testN->enemy_robot.getY()+75;j++)
			{
				img->pixels[i][j] =0.50;
			}
		}

		float xf,yf;
		//draw direction;
		xf = testN->own_robot.getRotation();
		xf = xf / 57.2957795;
		xf = -sin(xf);
		xf = xf * 100;
		xf = xf + testN->own_robot.getX();

		yf = testN->own_robot.getRotation();
		yf = yf / 57.2957795;
		yf = cos(yf);
		yf = yf * 100;
		yf = yf + testN->own_robot.getY();

		DrawLine(img, testN->own_robot.getX(),testN->own_robot.getY(),(int)xf,(int)yf);




		xf = testN->enemy_robot.getRotation();
		xf = xf / 57.2957795;
		xf = -sin(xf);
		xf = xf * 100;
		xf = xf + testN->enemy_robot.getX();

		yf = testN->enemy_robot.getRotation();
		yf = yf / 57.2957795;
		yf = cos(yf);
		yf = yf * 100;
		yf = yf + testN->enemy_robot.getY();
			
		DrawLine(img, testN->enemy_robot.getX(),testN->enemy_robot.getY(),(int)xf,(int)yf);


		WritePGMFile(img_attack, img);




		scanf("%*c");

		x = testN->child->own_robot.getX();
		y = testN->child->own_robot.getY();
		r = testN->child->own_robot.getRotation();
		
		robot_ai->del_element(testN);	//alle dynamish gecreeerde noden verwijderen (otherwise memory leak really fast ! )
	}
}
